from flask import Blueprint, request, jsonify, session
from werkzeug.security import check_password_hash

auth_bp = Blueprint('auth', __name__)

# 测试账号
TEST_ACCOUNTS = {
    'admin': {'password': 'admin123', 'role': 'admin'},
    'customer1': {'password': 'customer1', 'role': 'customer'}
}

@auth_bp.route('/login', methods=['POST'])
def login():
    data = request.get_json()
    username = data.get('username')
    password = data.get('password')
    role = data.get('role')

    # 验证用户
    user = TEST_ACCOUNTS.get(username)
    if not user or user['password'] != password or user['role'] != role:
        return jsonify({'success': False, 'message': '用户名、密码或角色不匹配'}), 401

    # 返回用户信息
    return jsonify({
        'success': True,
        'message': '登录成功',
        'user': {
            'id': username,
            'name': username,
            'avatar': username[0].upper(),
            'is_admin': role == 'admin'
        }
    })

@auth_bp.route('/logout', methods=['POST'])
def logout():
    session.clear()
    return jsonify({'success': True})